package com.icinfo.cloud.provider.punish.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.utils.EpsoftStringUtil;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseBasicInfoVo;
import com.icinfo.framework.common.web.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录token转换
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/app/login")
public class LoginTokenTransferController {


    /**
     * 综合一体化执法监管数字应用门户或掌上执法（浙政钉）跳转待访问外部系统链接(跳转时候，携带登陆返回的token)
     *
     * @param token
     * @return {@link Result< CaseBasicInfoVo >}
     * @author cyy
     * @date 2022-02-22 09:33
     */
    @GetMapping("/index")
    public Result getCaseBasicInfo(@RequestParam String token) {
        // 接收掌上执法token,调用token验证接口
        Map<String,Object> map = new HashMap<>(16);
        map.put("token",token);
        map.put("accessToken","带申请");
        map.put("systype","APP");
        String param = JSON.toJSONString(map);
        String response = HttpUtil.createPost("http://115.238.48.67:2080/api/v2/member/sso/validateToken").body(param).execute().body();
        JSONObject result = JSON.parseObject(response, JSONObject.class);
        // 调用 auth 登录接口 拿到PC端 token 放入缓存，返回前端
        String pcToken = "";
        if (result.getInteger("code").equals(200)){
            map.clear();
            map.put("phone", EpsoftStringUtil.getEncryptString(result.getString("phone")));
            String paramLogin = JSON.toJSONString(map);
            String responseLogin = HttpUtil.createPost("http://xxx/api/v2/user/secretKeyLogin").body(paramLogin).execute().body();
            JSONObject resultLogin = JSON.parseObject(responseLogin, JSONObject.class);
            if (resultLogin.getInteger("code").equals(200)){
                pcToken = resultLogin.getString("data");
                // 存入缓存
                // todo
                // 重定向前端首页
                //todo
            }
        }
        return Result.success("success");
    }

}
